package com.example.xymarket.httpUtil;

import android.os.Message;

import com.example.xymarket.common.Info;
import com.example.xymarket.po.Merchant;
import com.example.xymarket.po.Result;
import com.example.xymarket.common.HttpCon;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.common.MyJson;
import com.example.xymarket.common.MyUrl;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MerchantService {

    MyHandler myHandler;
    public MerchantService(MyHandler myHandler1)
    {
        myHandler=myHandler1;
    }

    /**
     * 修改一下
     */
    public  void getMyMerchant()  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"merchant/get_my_merchant",null,head,null);
                    Type type = new TypeToken<Result<Merchant>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        Merchant merchantList= (Merchant) result.getData();
                        Info.setMyMerchant(new Merchant());
                        Info.setMyMerchant(merchantList);
                        Info.getMyMerchant().setDrawable(ImageService.getBitmap(MyUrl.ImagesUrl+Info.getMyMerchant().getImage()));
                        myHandler.getHandler().sendEmptyMessage(10);
                    }
                    else
                    {
                        myHandler.getHandler().sendEmptyMessage(-10);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public  void getAllMerchant()  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"merchant/mget",null,head,null);
                    Type type = new TypeToken<Result<List<Merchant>>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        List<Merchant> merchantList= (List<Merchant>) result.getData();
                        Message message=new Message();
                        message.obj=merchantList;
                        message.what=10;
                        myHandler.getHandler().sendMessage(message);
                    }
                    else
                    {
                        myHandler.getHandler().sendEmptyMessage(-10);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
